
//2765.最长交替子数组
class Solution {
public:
    int alternatingSubarray(vector<int>& nums) {
        int i=0,n=nums.size();
        int ret=-1;
        while(i<n)
        {
            while(i<n-1&&nums[i+1]-nums[i]!=1) i++;  //找到第一个满足条件的位置
            if(i==n-1) return ret;

            int start=i++;
            while(i<n-1&&nums[i-1]==nums[i+1]) i++;  //根据s[m - 1] - s[m - 2] = (-1)^m 
                                                    //可以推出nums[i-1]==nums[i+1]
            ret=max(ret,i-start+1);
        }
        return ret;
    }
};